package ioperation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class AddOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");
        //判满
        int currentSize = bookList.getUsedSize();
        if(currentSize == bookList.getBooks().length){
            System.out.println("书架满了，不能放了");
            return;
        }

        //构建对象
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要新增的书名");
        String name = scanner.nextLine();

        System.out.println("请输入要新增的图书的作者");
        String author = scanner.nextLine();

        System.out.println("请输入要新增图书的价格");
        int price = scanner.nextInt();
        scanner.nextLine();//读取换行符号
        System.out.println("请输入要新增的图书的类型");
        String type = scanner.nextLine();


        Book newbook = new Book(name,author,price,type);
        //判断是否有这本书
        for (int i = 0;i < currentSize; i++){
            if(bookList.getBook(i).getName().equals(name)){
                System.out.println(name+"已经被添加了");//✳✳✳✳
                return;
            }
        }
        bookList.setBook(currentSize,newbook);

        System.out.println("添加成功");

        bookList.setUsedSize(currentSize+1);

    }
}
